package com.dlcb.csd.controller;

import com.dlcb.csd.dto.ActionResult;
import com.dlcb.csd.po.User;
import com.dlcb.csd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @Author:薛亮亮
 * @Date 2020/12/22 23:19
 * @Discription:
 */
@RestController
public class UserContronller {
    @Autowired
    private UserService userService;



    @RequestMapping(value = "/register", method = RequestMethod.POST)
    /**
     * 注册功能
     */
    public ActionResult register(User user) {
        ActionResult actionResult = new ActionResult();
        User userByTel = userService.findUserByTel(user.getTel());

        if (userByTel != null) {
            actionResult.setInfo("账户已经存在");

        } else {

            int i = userService.insertUser(user);
            actionResult.setCode(i);

        }

        return actionResult;
    }

    /**
     * 登录功能
     *
     * @param tel
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)

    public ActionResult login(String tel, String password, HttpSession session, HttpServletRequest request) {
        ActionResult actionResult = new ActionResult();
        User user = userService.findUserByTel(tel);
        session.setAttribute("user", user);
        String userType = request.getParameter("login_user");
        if (user != null) {
            if (password.equals(user.getPassword())) {
                //登录成功
                if (userType.equals("sysUser")) {
                    actionResult.setCode(2001);
                } else {
                    actionResult.setCode(2002);
                }


            } else {
                //账户或密码不对
                actionResult.setInfo("账户或密码不对");
            }
        } else {
            //没有用户
            actionResult.setInfo("用户不存在");
        }


        return actionResult;
    }

    /**
     * 获取登录的手机号,显示在首页上
     *
     * @param session Session对象
     * @return 返回从session中查询到的对象
     */
    @RequestMapping(value = "/getTel", method = RequestMethod.GET)

    public User getTel(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return user;
    }

    /**
     * 查询后台用户列表
     *
     * @return
     */

    @RequestMapping(value = "/getSysUser", method = RequestMethod.GET)
    public List<User> getSysUser() {
//        System.out.println("------------------");
        List<User> users = userService.findSysUser();

        return users;

    }

    /**
     * 删除后台用户
     *
     * @param id
     * @return
     */

    @RequestMapping(value = "/delUser", method = RequestMethod.DELETE)
    public ActionResult deleteUserById(int id) {
        System.out.println(id);
        System.out.println("hahahha");
        boolean bl = userService.deleteUserById(id);
        return new ActionResult(200, null, "删除成功");
    }

    @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
    public ActionResult getUserById(@PathVariable("id") int id) {
        User user = userService.findUserById(id);
        return new ActionResult(200, user, null);
    }


}
